﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace FFmpegVideoClip.Lib.UtilityControlsTools
{
    [ValueConversion(typeof(object[]), typeof(string))]
    public class ProgressBarToStringConvert : IMultiValueConverter
    {
        //public static double Clamp01(double value) => value < 0 ? 0 : value > 1 ? 1 : value;
        //public static double InverseLerp(double a, double b, double value) => a == b ? 0 : Clamp01((value - a) / (b - a));
        public static bool VerifyParametersValidity(object[] values)
        {
            if (values.Length != 2)
                return false;
            object a = values[0], b = values[1];
            if (a is null || b is null || a == DependencyProperty.UnsetValue || b == DependencyProperty.UnsetValue)
                return false;
            return true;
        }
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            //编辑模式下，始终非法
            if (!VerifyParametersValidity(values))
                return "参数非法";
            double Max = (double)values[0], Value = (double)values[1];
            return $"{Math.Round(Value / Max * 100, 2, MidpointRounding.AwayFromZero)}%";
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException($"{nameof(ProgressBarToStringConvert)}不支持逆向转换");
        }
    }
}
